package com.gexiaobao.pigeon.ui.fragment.mall

import android.graphics.Color
import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.model.bean.GoodsTypeParentResponse
import com.gexiaobao.pigeon.app.util.RxToast
import com.gexiaobao.pigeon.app.util.StatusBarUtil
import com.gexiaobao.pigeon.databinding.FragmentProductClassificationBinding
import com.gexiaobao.pigeon.ui.adapter.MyPagerAdapter
import com.gexiaobao.pigeon.ui.mall.MallViewModel
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.navigateAction
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat

/**
 * @Author      : hxw
 * @Date        : 2023/12/7 11:41
 * @Describe    : 商品列表分类
 */
class FragmentProductClassification : BaseFragment<MallViewModel, FragmentProductClassificationBinding>() {

    private var mIndex = -1

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        activity?.let { StatusBarUtil.setLightMode(it) }
        activity?.let { StatusBarUtil.setColor(it, Color.TRANSPARENT, 0) }
    }

    override fun initView(savedInstanceState: Bundle?) {
        mIndex = requireArguments().getInt("index")
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.llSearch, mDatabind.etSearchTitle, mDatabind.ivBack) {
            when (it) {
                mDatabind.ivBack -> {
                    nav().navigateUp()
                }

                mDatabind.llSearch, mDatabind.etSearchTitle -> {
                    nav().navigateAction(R.id.action_to_fragment_search_goods)
                }
            }
        }
    }

    override fun lazyLoadData() {
        super.lazyLoadData()
        mViewModel.getGoodTypeFatherList("0")
    }

    override fun createObserver() {
        super.createObserver()
        /** 获取分类父列表  */
        mViewModel.goodsTypeParentResult.observe(viewLifecycleOwner) {
            showGoodsTypeTab(it)
        }
    }

    private fun showGoodsTypeTab(resultData: GoodsTypeParentResponse) {
        if (resultData.isEmpty() || resultData.list.size == 0) {
            RxToast.showToast("获取分类产品失败")
            return
        }

        val titleList = ArrayList<String>()
        val fragmentList = ArrayList<Fragment>()
        for (index in 0 until resultData.list.size) {
            titleList.add(resultData.list[index].goodTypeName)
//            fragmentList.add(FragmentMallProduct.newInstance(resultData.list[index].id.toString()))
            fragmentList.add(FragmentMallShop.newInstance(resultData.list[index].id.toString()))
        }

        mDatabind.viewpager.adapter = context?.let { MyPagerAdapter(childFragmentManager, it, fragmentList, titleList) }
        mDatabind.viewpager.offscreenPageLimit = resultData.list.size
        mDatabind.tabLayout.setupWithViewPager(mDatabind.viewpager)
        mDatabind.viewpager.currentItem = mIndex

        mDatabind.viewpager.setOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {}
            override fun onPageSelected(position: Int) {}
            override fun onPageScrollStateChanged(state: Int) {}
        })
    }
}